package com.yidu.verycd_background.controller;
import com.yidu.verycd_background.entity.VerycdMovie;
import com.yidu.verycd_background.entity.VerycdType;
import com.yidu.verycd_background.service.VerycdTypeService;
import com.yidu.verycd_background.uilt.Msg;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (VerycdType)表控制层
 *
 * @author makejava
 * @since 2020-07-27 09:53:15
 */
@RestController
@RequestMapping("verycdType")
public class VerycdTypeController {
    /**
     * 服务对象
     */
    @Resource
    private VerycdTypeService verycdTypeService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne")
    public VerycdType selectOne(Integer id) {
        return this.verycdTypeService.queryById(id);
    }


    @GetMapping("selectAll")
    public Map selectAll(String page, String limit, String name){
        System.out.println("+++++++++++++++++++++");
        Integer pager=0;
        Integer limits=0;
        if(page!=null){
            pager = Integer.valueOf(page);
        }
        if(limit!=null) {
            limits = Integer.valueOf(limit);
        }
        List<VerycdType> movieList=verycdTypeService.selectAll(pager,limits,name);
        Integer count =verycdTypeService.paging(name);
        Map<String , Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("count",count);
        map.put("data", movieList);
        map.put("msg", "");
        return map;
    }

    @PostMapping("delete")
    public Msg delete(String id){
        Msg msg=new Msg();
        Integer row=verycdTypeService.deleteById(Integer.valueOf(id));
        if (row>0){
            msg.setState(1);
            msg.setMsg("删除成功");
        }else {
            msg.setState(0);
            msg.setMsg("删除失败");
        }
        return msg;
    }


    @PostMapping("addOrUpdate")
    public Msg addOrUpdate(VerycdType type) throws ParseException {
        Msg msg=new Msg();
        Integer row=0;
        if (type.getTypeId()==null){
            row=verycdTypeService.insert(type);
        }else {
            row=verycdTypeService.update(type);
        }
        if (row>0){
            msg.setState(1);
            msg.setMsg("数据修改成功");
        }else{
            msg.setState(0);
            msg.setMsg("数据修改失败");
        }
        return msg;
    }



}